home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / editor / auror300.zip / MACHINE.AML < prev    next >
Text File  |  1996-07-17  |  4KB  |  123 lines

  1. //--------------------------------------------------------------------
  2. // MACHINE.AML
  3. // Machine Code Interface Examples, (C) 1993-1996 by nuText Systems
  4. //
  5. // This macro shows how to call machine code written in assembly
  6. // language, and how to pass arguments and retrieve return values
  7. // (see also Machine.asm and documentation for the builtin 'machine'
  8. // function).
  9. //
  10. // See each example for details.
  11. //
  12. // Usage:
  13. //
  14. // Select this macro from the Macro List (on the Macro menu), or run it
  15. // from the macro picklist <shift f12>.
  16. //--------------------------------------------------------------------
  17.  
  18. include bootpath "define.aml"
  19.  
  20. // This function returns the contents of a .Bin file (in the same path
  21. // as the this source file) as a macro language string at compile-time.
  22. constant function binaryfile (file)
  23.   sourcefile = getcurrfile
  24.   binfile = sourcefile [1..pos "\\" sourcefile 'r'] + file + ".bin"
  25.   if (openfile binfile) <> -1 then
  26.     binstring = readfile
  27.     closefile
  28.     // .BIN file must be less than 16000 bytes
  29.     if length binstring < 16000 then
  30.       return binstring
  31.     end
  32.   end
  33.   seterror 1501 file
  34. end
  35.  
  36.  
  37. variable arg1, arg2, arg3, value
  38.  
  39. // display results
  40. private function results (test)
  41.   msgbox "Machine Code Test Results #" + test + ":\n" +
  42.          "(see Machine.aml/Machine.asm)\n" +
  43.          ("\nReturn = " + value) :-40 +
  44.          ("\nArg 1  = " + arg1)  :-40 +
  45.          ("\nArg 2  = " + arg2)  :-40 +
  46.          ("\nArg 3  = " + arg3)  :-40
  47. end
  48.  
  49.  
  50. //-------------------------------------------------------------------
  51. // Example #1 -- Executing Embedded Machine Code
  52. //-------------------------------------------------------------------
  53.  
  54. // set up test arguments for the call to 'machine'
  55. arg1 = " Test string"
  56. arg2 = 37
  57. arg3 = 14
  58.  
  59. // Execute the machine code. Note that when this macro is compiled,
  60. // the 'binaryfile' compile-time function will load the contents of
  61. // Machine.bin and embed it as a string in Machine.x.
  62. value = machine (binaryfile "machine") arg1 arg2 ref arg3
  63.  
  64. // display the results
  65. results 1
  66.  
  67.  
  68. //-------------------------------------------------------------------
  69. // Example #2 -- Embedding Machine Code in a Function
  70. //-------------------------------------------------------------------
  71.  
  72. // Wrapping a function definition around the machine call makes it
  73. // reusable and more convenient to use, and also ensures that a fixed
  74. // number of arguments are passed.
  75.  
  76. private function testcall (str num numref)
  77.   machine (binaryfile "machine") str num ref numref
  78. end
  79.  
  80. // set up test arguments
  81. arg1 = " Test string"
  82. arg2 = 37
  83. arg3 = 14
  84.  
  85. // execute the machine code
  86. value = testcall arg1 arg2 ref arg3
  87.  
  88. // display the results
  89. results 2
  90.  
  91.  
  92. //-------------------------------------------------------------------
  93. // Example #3 -- Dynamic Loading and Execution of Machine Code
  94. //-------------------------------------------------------------------
  95.  
  96. // This run-time function returns the contents of a .Bin file (in the same
  97. // path as this executable file) as a macro language string.
  98. private function loadbinary (file)
  99.   xfile = getcurrfile
  100.   binfile = xfile [1..pos "\\" xfile 'r'] + file + ".bin"
  101.   if (openfile binfile) <> -1 then
  102.     binstring = readfile
  103.     closefile
  104.     // .BIN file must be less than 16000 bytes
  105.     if (length binstring) < 16000 then
  106.       return binstring
  107.     end
  108.   end
  109. end
  110.  
  111. // set up test arguments for the call to 'machine'
  112. arg1 = " Test string"
  113. arg2 = 37
  114. arg3 = 14
  115.  
  116. // Execute the machine code. The 'loadbinary' function will load
  117. // the contents of Machine.bin at run-time, and the 'machine' function
  118. // will execute it.
  119. value = machine (loadbinary "machine") arg1 arg2 ref arg3
  120.  
  121. // display the results
  122. results 3
  123.